<?php
namespace App\Services\Adv;
use App\Services\Adv\Adv\AdvClassifyLogic;
use App\Services\Manage\ManageIndex;
/**
 * @Author: ws
 * @Date:   2018-06-12 16:23:45
 * @Last Modified by:   Marte
 */ 
class AdvClassify
{

    private function _baseCheckCate($data, bool $isEdit): array 
    {
        $returnRes = [];
        //获得总后台的APP_id
        $manageAppId = ManageIndex::Api()->getAppId();
        //修改分类时
        if($isEdit){
            $validateInitData = $isEdit ? ['requiredId' => true] : [];
            ValidateAdvClassify::checkValidate($data, ['name', 'app_id', 'pid', 'action'], true, $validateInitData);
            $cateInfo = (new AdvClassifyLogic())->dbCateInfo($data);
            if(!$cateInfo){ return verify_err(null, '该分类不存在'); }
            if($data['app_id'] != $manageAppId && $data['app_id'] != $cateInfo['app_id']){ return verify_err(null, '该app_id无权操作该分类'); }
            if($cateInfo['pid']){  
                if(isset($data['other_app_read']) && $data['other_app_read'] != $cateInfo['other_app_read']){ return verify_err(null, '不可修改子类的other_app_read'); }
            }
            if(isset($data['action']) && $data['action'] != $cateInfo['action']){
                $checkAction = (new AdvClassifyLogic())->checkAction($data);
                if($checkAction['status'] != SUCCESS_STATUS){ return verify_err(null, '该识别码action已存在'); }
            }
            if($manageAppId != $data['app_id'] && isset($data['is_public']) && $data['is_public'] == 1){ return verify_err(null, '公共分类的所属应用必须为亚数后台'); }
            $returnRes = $cateInfo;
        }else{//添加分类时
            ValidateAdvClassify::checkValidate($data, ['name', 'app_id', 'pid', 'action']);
            if($manageAppId != $data['app_id'] && $data['is_public'] == 1){ return verify_err(null, '只有亚数后台才可以添加公共应用的分类'); }
            if($data['pid']){
                $parentInfo = (new AdvClassifyLogic())->dbCateInfo(['id'=>$data['pid']]);
                if(!$parentInfo || $parentInfo['status'] != 1){ return verify_err(null, '该父类不存在或该父类的状态不正常'); }
                if($parentInfo['app_id'] != $data['app_id'] || $parentInfo['other_app_read'] != (string)$data['other_app_read']){ return verify_err(null, '子类的app_id和other_app_read需和父类保持一致'); }
                $returnRes = $parentInfo;
            }
            if(isset($data['action'])){
                $checkAction = (new AdvClassifyLogic())->checkAction($data);
                if($checkAction['status'] != SUCCESS_STATUS){ return verify_err(null, '该识别码action已存在'); }
            }
        }
        return auto_success($returnRes, '可操作');
    }


    // 广告位分类添加
    public function add($data)
    {
        if (empty($data)) {
            return null_data($data,"参数不能为空！");
        }
        $data = init_array($data,["name"=>"", "app_id"=>"", "other_app_read"=>" ", "is_public"=>0, "action"=>"","pid"=>0, "order"=>0, "description"=>" ", "status"=>1, "add_user_id"=>""]);
        ValidateAdvClassify::checkValidate($data, ['name', 'app_id', 'pid', 'action']);
        $checkData = $this->_baseCheckCate($data, false);
        if ($checkData['status'] != SUCCESS_STATUS) {
            return $checkData;
        }
        return (new AdvClassifyLogic())->advClassifyAdd($data);
    }
    // 获取某应用全部广告位分类列表 树状
    public function classifyList($data)
    {
        return (new AdvClassifyLogic())->advClassifyList($data);
    }
    // 广告位分类编辑
    public function edit($data): array
    {
        if (empty($data)) {
            return null_data($data,"参数不能为空！");
        }
        $data = init_array($data,["id" => " ", "name" => "", "app_id" => "", "other_app_read" => " ",
            "is_public" => 0, "action" => "", "pid" => 0, "order" => 0, "description" => " ", "status" => "1","add_user_id" => ""]);
        ValidateAdvClassify::checkValidate($data, ['id', 'action']);
        $checkData = $this->_baseCheckCate($data, true);
        if ($checkData['status'] != SUCCESS_STATUS) {
            return $checkData;
        }
        return (new AdvClassifyLogic())->advClassifyEdit($data);
    }

     // 启用某个分类广告位分类
    public function classifyEnable($data)
    {
        $data = init_array($data, ['id' => 0]);
        ValidateAdvClassify::checkValidate($data, ['id']);
        return (new AdvClassifyLogic())->advClassifyEnable($data);
    }
    // 禁用某个分类广告位分类
    public function classifyDisable($data)
    {
        $data = init_array($data, ['id' => 0]);
        ValidateAdvClassify::checkValidate($data, ['id']);
        return (new AdvClassifyLogic())->advClassifyDisable($data);
    }
     // 删除某个分类广告位分类
    public function classifyDelete($data)
    {
        $data = init_array($data, ['id' => 0]);
        ValidateAdvClassify::checkValidate($data, ['id']);
        return (new AdvClassifyLogic())->advClassifyDelete($data);
    }
     // 某一类广告位分类列表获取
    public function classifySomeList($data)
    {
         $data = init_array($data, ['pid' => 0,'app_id' => 0]);
         ValidateAdvClassify::checkValidate($data, ['pid','app_id']);
        return (new AdvClassifyLogic())->advClassifySomeList($data);
    }
     // 不同条件广告位分类列表获取 (分页)
    public function classifyCondition($data)
    {
         $data = init_array($data, ['keywords' => 0,'page' => 1, 'listRows' => 15]);
         ValidateAdvClassify::checkValidate($data, ['page','listRows']);
         if (empty($data['keywords'])) {
            return null_data($data,"参数不能为空！");
        }
         return (new AdvClassifyLogic())->advClassifyCondition($data);
    }
    // 获取某广告位分类详情
    public function classifyInfo($data)
    {
        if (empty($data)) {
            return null_data($data,"参数不能为空！");
        }
        $data = init_array($data, ['id' => 0]);
        ValidateAdvClassify::checkValidate($data, ['id']);
        return (new AdvClassifyLogic())->classifyInfo($data);
    }
}
